package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.INavService;
import com.cn.wanxi.util.IOTool;
import com.cn.wanxi.util.Tool;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NavServiceImpl implements INavService {
    private static final String SPILT = "\n";
    private static final String PATH = "E:\\wanxi\\step-3\\canyin03-1011\\web";
    private static final String DIRECTORY = "note";
    private static final String FILE_NAME = "nav.txt";

    @Override
    public NavModel[] getNavModels() {
//        1.创建文件
//        不然别人可以创建对象调用：
        File file = IOTool.createFile(PATH, DIRECTORY, FILE_NAME);
//        2.封装dao层数据
        List<String> data = setDaoData();
//        3.写入文件
        IOTool.writeData(file, data);
//        4.读取文件
        String string = String.valueOf(IOTool.readData(file));
//        5.封装对象
        NavModel[] navModels = setData(string);
        return navModels;
    }

    private NavModel[] setData(String string) {
        System.out.println(string);
//        NavDao navDao = new NavDao();
//        String href="/home/company/news";
//        String title="网站首页公司信息新闻信息";
        String[] results = string.split(SPILT);

//        String href = results[0];//[/home, /company, news.html]
//        String title = results[1];//[网站首页, 公司信息, 新闻信息]
//        需要将href和title里面的[]去掉，删除
//        java 删除 其实是替换   将[]替换为空字符串 ''
//        href = href.replace("[", "");
//        href = href.replace("]", "");
        String href = Tool.replace(results[0]);
        String title = Tool.replace(results[1]);
        String[] hrefs = href.split(",");
        String[] titles = title.split(",");
        int length = hrefs.length;
        NavModel[] navModels = new NavModel[length];
        for (int i = 0; i < length; i++) {
            NavModel model = new NavModel();
            model.setHref(hrefs[i]);
            model.setTitle(titles[i]);
            navModels[i] = model;
        }
        return navModels;
    }


    private List<String> setDaoData() {
        NavDao navDao = new NavDao();
        List<String> list = new ArrayList<>();
//        for (int i = 0; i < navDao.hrefs.length; i++) {
//        将数组转换为字符串？
//        String[]  aa={};
        list.add(Arrays.toString(navDao.hrefs));
        list.add(Arrays.toString(navDao.titles));
//        }
        return list;
    }


    public static void main(String[] args) {
        new NavServiceImpl().getNavModels();
    }
}
